import os
from datetime import timedelta


class ToDictClass:
    @classmethod
    def to_dict(cls):
        return {
            name: value for name, value in vars(cls).items() if not name.startswith("_")
        }


class RedisConfig(ToDictClass):
    host = os.environ["REDIS_HOST"]
    port = os.environ["REDIS_PORT"]
    db = os.environ["REDIS_DB"]


class MysqlConfig(ToDictClass):
    host = os.environ["MYSQL_HOST"]
    port = os.environ["MYSQL_PORT"]
    username = os.environ["MYSQL_USER_NAME"]
    password = os.environ["MYSQL_PASSWORD"]
    db_name = os.environ["MYSQL_DB_NAME"]


class Config:
    # the form is {db_type}://{username}:{password}@{host}:{port}/{db_name}?{param1}=value&{plaram2}=value
    REDIS_URL = (
        f"redis://{RedisConfig.host}:"
        f"{RedisConfig.port}/"
        f"{RedisConfig.db}?encoding=utf-8"
    )
    MYSQL_URL = (
        f"mysql://{MysqlConfig.username}:"
        f"{MysqlConfig.password}@"
        f"{MysqlConfig.host}:"
        f"{MysqlConfig.port}/"
        f"{MysqlConfig.db_name}"
    )
    hash_id_salt = os.environ["HASH_ID_SALT"]
    SECRET_KEY = os.environ["SECRET_KEY"]
    ALGORITHM = "HS256"
    TOKEN_EXPIRE_TIME = timedelta(days=1)
    CACHE_TIMEOUT = 3600 * 12
    SERVER_NAME = "MyBlog"
    MAX_CONTENT_LENGTH = 4096
